package com.wiscamp.ninechapters.tags.infrastructure.mappers;

import com.wiscamp.ninechapters.tags.infrastructure.po.KnowledgePointRelationPO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;


/**
 * 学科知识点仓储对象
 *
 * @author yifeng
 */
@Repository
public interface KnowledgePointRelationMapper extends JpaRepository<KnowledgePointRelationPO, Long>, JpaSpecificationExecutor<KnowledgePointRelationPO> {

    /**
     * 通过源学科知识点和目标学科知识点编号获取关联列表
     *
     * @param sourceId
     * @param targetId
     * @return
     */
    @Query(value = "select e from KnowledgePointRelationPO e where e.sourcePointId = :sourceId and e.targetPointId = :targetId and e.relationTypeId = :relationTypeId")
    KnowledgePointRelationPO findRelationBySourceAndTarget(long sourceId, long targetId, int relationTypeId);

    @Query(value = "select e from KnowledgePointRelationPO e where e.sourcePointId = :sourceId")
    Collection<KnowledgePointRelationPO> findRelationsBySource(long sourceId);

    @Query("select spr.sourcePointId from KnowledgePointView e, KnowledgePointRelationPO spr where e.pointId = spr.targetPointId and spr.targetPointId in (:ids) and spr.relationTypeId = :relationTypeId")
    Collection<Long> findPointIdsByRelationType(Collection<Long> ids, int relationTypeId);
}